<template>
  <div>
    <el-card class="box-card" style="margin-top: 0;">
      <div slot="header" class="clearfix">
        <span>筛选查询</span>
        <el-button
          type="primary"
          size="mini"
          style="float: right"
          @click="handleAdd"
          v-hasPermi="['campSystem:campManager:invite:add']"
        >添加邀请码
        </el-button>
        <el-button
          type="primary"
          size="mini"
          style="float: right;margin-right: 10px;"
          @click="handleAddTo"
          v-hasPermi="['campSystem:campManager:invite:add']"
        >追加邀请码
        </el-button>
      </div>
      <div class="text item">
        <el-form
          :model="queryParams"
          ref="queryForm"
          :inline="true"
        >
          <el-form-item label="" prop="invName">
            <el-select
              v-model="queryParams.invName"
              placeholder="请选择邀请码名称"
              clearable
              size="small"
            >
              <el-option
                v-for="(item, index) in codeNamesOptions"
                :key="index"
                :label="item"
                :value="item"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="" prop="invUsed">
            <el-select
              v-model="queryParams.invUsed"
              placeholder="请选择邀请码状态"
              clearable
              size="small"
            >
              <el-option
                v-for="(item, index) in codeOptions"
                :key="index"
                :label="item.dictLabel"
                :value="Number(item.dictValue)"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="" prop="invCode">
            <el-input
              v-model="queryParams.invCode"
              placeholder="请输入邀请码ID"
              clearable
              size="small"
            />
          </el-form-item>

          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
            >搜索
            </el-button
            >
            <el-button
              type="primary"
              icon="el-icon-refresh"
              size="mini"
              @click="resetQuery"
            >重置
            </el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </el-card>

    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>数据列表</span>

        <el-button
          type="primary"
          size="mini"
          style="float: right; margin-left: 10px;"
          @click="handleExport"
          v-hasPermi="['campSystem:campManager:invite:export']"
        >导出列表
        </el-button>
      </div>
      <div class="text item">
        <el-table v-loading="loading" :data="tableList">
          <el-table-column label="邀请码名称" align="center" prop="invName"/>
          <el-table-column label="邀请码ID" align="center" prop="invCode"/>
          <el-table-column label="减免金额" align="center" prop="invReduction"/>
          <el-table-column label="邀请码状态" align="center" width="100" :formatter="codeFormat"/>
          <el-table-column label="姓名" align="center" prop="stuName"/>
          <el-table-column label="手机号" align="center" prop="phone"/>
          <el-table-column label="报名状态" align="center" prop="status"/>
        </el-table>
      </div>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>
    <add-invite-code :dialogVisibleFu="dialogVisibleFu" :typeFu="typeFu" @closeDialog="closeDialog"></add-invite-code>
  </div>
</template>

<script>
  import {GetInviteQuery, GetInviteList} from "@/api/campSystem/campOper";
  import AddInviteCode from "@/components/CampSystem/CampOper/AddInviteCode"

  export default {
    name: "InvitationCode",
    props: {
      idFu: [Number, String]
    },
    components: {
      AddInviteCode
    },
    data() {
      return {
        loading: false,// 表格加载loading
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          campsiteId: null,
          invName: null,
          invUsed: null,
          invCode: null
        },
        total: 0,// 数据总条数
        tableList: [], // 表格数据列表
        codeOptions: [], // 营地邀请码状态列表
        codeNamesOptions: [], // 营地邀请码名称列表
        dialogVisibleFu: false, // 控制添加邀请码弹框显示与隐藏
        typeFu: null// 类型 1-添加邀请码，2-追加邀请码
      };
    },
    created() {
      this.queryParams.campsiteId = this.idFu;
      this.getDicts("camp_code_status").then(response => {
        this.codeOptions = response.data;
      });
      this.getInviteQuery();
      this.getList();
    },
    methods: {
      // 关闭添加邀请码弹框，子组件触发
      closeDialog(val, type) {
        this.dialogVisibleFu = val;
        if (type) {
          this.getList();
          this.getInviteQuery();
        }
      },
      // 追加邀请码按钮
      handleAddTo() {
        this.typeFu = 2;
        this.dialogVisibleFu = true;
      },
      // 添加邀请码按钮
      handleAdd() {
        this.typeFu = 1;
        this.dialogVisibleFu = true;
      },
      // 重置按钮
      resetQuery() {
        this.$refs["queryForm"].resetFields();
        this.handleQuery();
      },
      // 搜索按钮
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.queryParams.pageSize = 10;
        this.getList();
      },
      // 获取列表
      getList() {
        this.loading = true;
        GetInviteList(this.queryParams)
          .then(response => {
            console.log(response);
            if (response.code != 200) {
              this.loading = false;
              return this.msgError(response.msg);
            }
            this.tableList = response.rows;
            this.total = response.total;
            this.loading = false;
          })
          .catch(error => {
            console.log(error);
            this.loading = false;
          });
      },
      // 字典状态字典翻译-邀请码状态
      codeFormat(row, column) {
        return this.selectDictLabel(this.codeOptions, row.invUsed);
      },
      // 获取指定营地邀请码筛选列表
      getInviteQuery() {
        GetInviteQuery({
          campsiteId: this.idFu
        })
          .then(response => {
            console.log(response);
            if (response.code != 200) {
              return this.msgError(response.msg);
            }
            this.codeNamesOptions = response.data;
          })
          .catch(error => {
            console.log(error);
          });
      },

      /** 导出按钮操作 */
      handleExport() {
        this.download(
          "modules-campsite/code/export",
          {
            campsiteId: this.queryParams.campsiteId
          },
          `邀请码列表.xlsx`
        );
      },
    },
    watch: {
      idFu(val) {
        this.queryParams.campsiteId = val;
        this.getList();
      },
    }
  }
</script>

<style lang="scss">
  @import "@/assets/styles/card.scss";
</style>


